package com.taoyuanx.utils;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


/**
 * @author ll 读取配置文件
 */
public final class PropertiesUtil {
	private static Properties pro=null;
	private static String default_properties="config/config.properties";
	private static String env = "dev";//默认开发环境
	static{
		try {
			Resource resource=new ClassPathResource(default_properties);
			EncodedResource encodedResource=new EncodedResource(resource,"UTF-8");
			pro=PropertiesLoaderUtils.loadProperties(encodedResource);
			env=pro.getProperty("environment")+".";
		} catch (IOException e) {
			System.out.println("加载属性文件[" + default_properties + "]失败：" + e.getMessage());
		}
	}
	//获取非默认配置文件
	public static Properties getProperties(String configPath) throws IOException{
		Resource resource=new ClassPathResource(configPath);
		EncodedResource encodedResource=new EncodedResource(resource,"UTF-8");
		return PropertiesLoaderUtils.loadProperties(encodedResource);
	}
	public static String getSystemProperty(String key) {
		if(null == pro) return null;
		//如果公共配置中包含，以公共配置优先
		if(pro.containsKey(key))return pro.getProperty(key);
		return pro.getProperty(env+key);
	}
	public static String getSystemProperty(String key,String defaultValue) {
		if(null == pro) return null;
		//如果公共配置中包含，以公共配置优先
		if(pro.containsKey(key))return pro.getProperty(key,defaultValue);
		return pro.getProperty(env+key,defaultValue);
	}
	public static void main(String[] args) {
		System.out.println(getSystemProperty("system_p12_path"));
	}

}
